using System;
using System.Text;
using TccAg.VO;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using System.Data.SqlClient;

namespace TccAg.DA
{
    public class GradeAcademicaDA
    {
        private String queryInsert = "INSERT INTO grade_academica( PERACAD, CURSO, GERACAO, NOTA) VALUES( @peracad, @curso, @geracao, @nota)";
        private String queryUpdate = "UPDATE grade_academica SET peracad = @peracad, curso = @curso, geracao = @geracao, nota = @nota WHERE ID = @id";
        private String queryDelete = "DELETE FROM grade_academica WHERE ID = @id";
        private String queryLoadAll = "SELECT  id, peracad, curso, geracao, nota FROM grade_academica";
        private String queryLoadByPk = "SELECT  id, peracad, curso, geracao, nota FROM grade_academica WHERE ID = @id";
        private String queryLoadByFk = "SELECT  id, peracad, curso, geracao, nota FROM grade_academica WHERE PERACAD = @peracad, CURSO = @curso";
        private SqlConnection conexao = new SqlConnection("Pegar conexão");

        //INSERIR 
        public int inserir(GradeAcademicaVO gradeacademica)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryInsert;

                if (gradeacademica.Peracad.Id != 0)
                        cmd.Parameters.Add(new SqlParameter("@peracad", (object) gradeacademica.Peracad));
                else
                        cmd.Parameters.Add(new SqlParameter("@peracad", (object) DBNull.Value));

                if (gradeacademica.Curso.Id != 0)
                        cmd.Parameters.Add(new SqlParameter("@curso", (object) gradeacademica.Curso));
                else
                        cmd.Parameters.Add(new SqlParameter("@curso", (object) DBNull.Value));

                if (gradeacademica.Geracao != 0)
                        cmd.Parameters.Add(new SqlParameter("@geracao", (object) gradeacademica.Geracao));
                else
                        cmd.Parameters.Add(new SqlParameter("@geracao", (object) DBNull.Value));

                if (gradeacademica.Nota != 0)
                        cmd.Parameters.Add(new SqlParameter("@nota", (object) gradeacademica.Nota));
                else
                        cmd.Parameters.Add(new SqlParameter("@nota", (object) DBNull.Value));

                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //ATUALIZAR 
        public int atualizar(GradeAcademicaVO gradeacademica)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                if (gradeacademica.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)gradeacademica.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryUpdate;

                if (gradeacademica.Peracad.Id != 0)
                        cmd.Parameters.Add(new SqlParameter("@peracad", (object) gradeacademica.Peracad));
                else
                        cmd.Parameters.Add(new SqlParameter("@peracad", (object) DBNull.Value));
                if (gradeacademica.Curso.Id != 0)
                        cmd.Parameters.Add(new SqlParameter("@curso", (object) gradeacademica.Curso));
                else
                        cmd.Parameters.Add(new SqlParameter("@curso", (object) DBNull.Value));
                if (gradeacademica.Geracao != 0)
                        cmd.Parameters.Add(new SqlParameter("@geracao", (object) gradeacademica.Geracao));
                else
                        cmd.Parameters.Add(new SqlParameter("@geracao", (object) DBNull.Value));
                if (gradeacademica.Nota != 0)
                        cmd.Parameters.Add(new SqlParameter("@nota", (object) gradeacademica.Nota));
                else
                        cmd.Parameters.Add(new SqlParameter("@nota", (object) DBNull.Value));
                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //EXCLUIR 
        public int excluir(GradeAcademicaVO gradeacademica)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryDelete;

                if (gradeacademica.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)gradeacademica.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD ALL 
        public List<GradeAcademicaVO> loadAll()
        {
            try
            {
                List<GradeAcademicaVO> objRetorno = new List<GradeAcademicaVO>();
                GradeAcademicaVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadAll;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objTemp = new GradeAcademicaVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["peracad"] != DBNull.Value)
                           objTemp.Peracad.Id = (int)dr["peracad"];

                        if (dr["curso"] != DBNull.Value)
                           objTemp.Curso.Id = (int)dr["curso"];

                        if (dr["geracao"] != DBNull.Value)
                           objTemp.Geracao = (int)dr["geracao"];

                        if (dr["nota"] != DBNull.Value)
                           objTemp.Nota = (Double?)dr["nota"];

                        objRetorno.Add(objTemp);
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD BY PK - Passa um objeto do tipo VO para em casos onde usar chave primária composta ter essas propriedades preenchidas
        public GradeAcademicaVO loadByPk(GradeAcademicaVO gradeacademica)
        {
            try
            {
                GradeAcademicaVO objRetorno = null;
                GradeAcademicaVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                if (gradeacademica.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)gradeacademica.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByPk;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        objTemp = new GradeAcademicaVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["peracad"] != DBNull.Value)
                           objTemp.Peracad.Id = (int)dr["peracad"];

                        if (dr["curso"] != DBNull.Value)
                           objTemp.Curso.Id = (int)dr["curso"];

                        if (dr["geracao"] != DBNull.Value)
                           objTemp.Geracao = (int)dr["geracao"];

                        if (dr["nota"] != DBNull.Value)
                           objTemp.Nota = (Double?)dr["nota"];

                        objRetorno = objTemp;
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD BY FK - Passa um objeto do tipo VO para em casos onde usar chave estrangeira composta ter essas propriedades preenchidas
        public List<GradeAcademicaVO> loadByFk(GradeAcademicaVO gradeacademica)
        {
            try
            {
                List<GradeAcademicaVO> objRetorno = new List<GradeAcademicaVO>();
                GradeAcademicaVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                if (gradeacademica.Peracad.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@peracad", (object)gradeacademica.Peracad));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                if (gradeacademica.Curso.Id != 0)
                    cmd.Parameters.Add(new SqlParameter("@curso", (object)gradeacademica.Curso));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByFk;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        objTemp = new GradeAcademicaVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["peracad"] != DBNull.Value)
                           objTemp.Peracad.Id = (int)dr["peracad"];

                        if (dr["curso"] != DBNull.Value)
                           objTemp.Curso.Id = (int)dr["curso"];

                        if (dr["geracao"] != DBNull.Value)
                           objTemp.Geracao = (int)dr["geracao"];

                        if (dr["nota"] != DBNull.Value)
                           objTemp.Nota = (Double?)dr["nota"];

                        objRetorno.Add(objTemp);
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //DATATABLE LOAD ALL 
        public DataTable dataTableLoadAll()
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadAll;

                conexao.Open();
                SqlDataAdapter dtAdpt = new SqlDataAdapter(cmd);
                DataTable dtTbl = new DataTable("tab_grade_academica");

                dtAdpt.Fill(dtTbl);
                return dtTbl;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

    }
}
